package ru.space.academy.exception;

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import ru.space.academy.dto.ExceptionDTO;

@ControllerAdvice
public class ExceptionAdvice {

    @ResponseBody
    @ExceptionHandler(EntityNotFoundException.class)
    @ResponseStatus(HttpStatus.NOT_FOUND)
    ExceptionDTO internalServerHandler(EntityNotFoundException ex) {
        if (ex.getEntityId() != 0) {
            return new ExceptionDTO("Запись под ID " + ex.getEntityId() + " : " + ex.getMessage());
        }
        return new ExceptionDTO(ex.getMessage());
    }

    @ResponseBody
    @ExceptionHandler(IncorrectInputDataException.class)
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    ExceptionDTO incorrectInputDataHandler(IncorrectInputDataException ex) {
        return new ExceptionDTO(ex.getMessage());
    }
}
